package org.ssmd3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ssmd3.pojo.User;
import org.ssmd3.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    UserService userService;

    /**
     * 打开登录页面
     * @return
     */
@RequestMapping(value = "/login")
    public String login(){


        return "/WEB-INF/user/login.jsp";
    }

    @RequestMapping(value = "/dologin")
    public String doLogin(Model model, HttpSession session,User user){

       User user1=userService.getUser(user.getUserName());
       if (user1!=null){
           if (user.getUserPassWord().equals(user1.getUserPassWord())){
                //在session里面添加用户状态信息
               session.setAttribute("uname",user1.getUserName());

               return "redirect:/home/index";//重定向到这个页面
               //return "forwards:/home/index";转发到这个页面


           }else {
               model.addAttribute("error","密码错误");
           }

       }else {
           model.addAttribute("error","该用户名不存在");
       }


        return "/WEB-INF/user/login.jsp";
    }

}
